﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EBEInventory_DAL;

public partial class Inventory_Items_Management_ListItems : System.Web.UI.Page
{
    InventoryEntities ctx = null;



    protected void Page_Load(object sender, EventArgs e)
    {
        ctx = InitConnection.Initialize();

        if (!IsPostBack)
        {

            Dictionary<string, string> subNodes = CatOps.GetSubCategories("R", ctx);
            Hidden1.Value = "";
            
            if (Session["SelectedCategory"] != null)
            Session["SelectedCategory"] = null;

            foreach (string node in subNodes.Keys)
            {
                TreeNode tn = new TreeNode(subNodes[node], node);

                TreeVu_Cats.Nodes[0].ChildNodes.Add(tn);
                tn.Collapse();
            }

            //rootNode.Collapse();

        }

        else
        {
            if (Session["SearchItem"] != null)
                GridItems.DataSource = Session["SearchItem"];

            else
            {
                if (Session["SelectedCategory"] != null)
                {
                    category c = (category)(Session["SelectedCategory"]);
                    GridItems.DataSource = CatOps.GetCategoryItems(c.CategoryID, ctx);
                }
            }
        }
        

        string s =Hidden1.Value;
        
        Page.DataBind();
    }


    protected void TreeVu_Cats_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (TreeVu_Cats.SelectedNode == null) return;

        category selCat = new category();
        selCat.CategoryID =TreeVu_Cats.SelectedNode.Value;
        selCat.CategoryName = TreeVu_Cats.SelectedNode.Text;
        Hidden1.Value = TreeVu_Cats.SelectedNode.Value;
        Session["SelectedCategory"] = selCat;

        if (TreeVu_Cats.SelectedNode.ChildNodes.Count == 0)
        {
            Dictionary<string, string> subNodes = CatOps.GetSubCategories(TreeVu_Cats.SelectedNode.Value, ctx);

            foreach (string node in subNodes.Keys)
            {
                TreeNode tn = new TreeNode(subNodes[node], node);
                tn.PopulateOnDemand = true;
                tn.SelectAction = TreeNodeSelectAction.Select;
                TreeVu_Cats.SelectedNode.ChildNodes.Add(tn);
            }

            TreeVu_Cats.SelectedNode.Expand();

        }
        
        GridItems.DataSource = CatOps.GetCategoryItems(TreeVu_Cats.SelectedNode.Value, ctx);
        
        
        DataBind();

    }

    protected void GridItems_SelectedIndexChanged(object sender, EventArgs e)
    {
        ItemInfo item = new ItemInfo();
        ItemAddInfo addinfo = new ItemAddInfo();
        item.ItemID = Convert.ToInt32(GridItems.SelectedRow.Cells[1].Text);
        item.ItemName = GridItems.SelectedRow.Cells[2].Text;
        item.AdditionalInfo = short.Parse(GridItems.SelectedRow.Cells[3].Text);
        addinfo.ItemID = item.ItemID;
        addinfo.SerialNo = GridItems.SelectedRow.Cells[4].Text;
        addinfo.Model = GridItems.SelectedRow.Cells[5].Text;
        addinfo.PartNo = GridItems.SelectedRow.Cells[6].Text;
        
        item.Unit = GridItems.SelectedRow.Cells[7].Text;
        item.CategoryID = GridItems.SelectedRow.Cells[8].Text;
        
        item.itemsinfo_1 = addinfo;

        Session["selItem"] = item;

        GridItems.DataSource = CatOps.GetCategoryItems(TreeVu_Cats.SelectedNode.Value, ctx);
        GridItems.DataBind();
    }

    protected void GridItems_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridItems.PageIndex = e.NewPageIndex;
        GridItems.DataBind();
    }

    protected void btn_Search_Click(object sender, EventArgs e)
    {
       GridItems.DataSource= Session["SearchItem"]= ItemsOps.SearchCategory(txt_ItemName.Text,txt_SerialNo.Text,txt_Model.Text,txt_PartNo.Text, ctx);
       GridItems.DataBind();
    }
}